/********************************************************************************
*                 Copyright (C) 2011 - Las Venturas Playground                  *
*                      http://sa-mp.nl | http://lvp-iv.com                      *
*                                                                               *
*    This program is free software: you can redistribute it and/or modify       *
*    it under the terms of the GNU General Public License as published by       *
*    the Free Software Foundation, either version 3 of the License, or          *
*    (at your option) any later version.                                        *
*                                                                               *
*    This program is distributed in the hope that it will be useful,            *
*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
*    GNU General Public License for more details.                               *
*                                                                               *
*    You should have received a copy of the GNU General Public License          *
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
*                                                                               *
*********************************************************************************
*                                                                               *
*  Las Venturas Playground - Core/Account/commands.nut                          *
*                                                                               *
*  This file holds all database related commands.                               *
*  @author: Matthias <matthias@sa-mp.nl>                                        *
*                                                                               *
********************************************************************************/

local cmdHandler = CommandHandler();

/**
 * This is the /stats command, it sends the user some information about his/her profile.
 * @author: Matthias <matthias@sa-mp.nl>
 *
 * @param playerid The playerid that used the command.
 * @param commandParams The parameters passed on to the command.
 * @param commandText The entire command as a string.
 */
local function cmdStats (playerid, cmdWords, cmdStr)
{  
    if (isRegistered[playerid] == false)
    {
        sendPlayerErrorMessage (playerid, "You have to be registered to check your stats! Use /register to register yourself.");
        return;
    }
    
    // We get the entire online time (saved in the database + current online time).
    local totalTime = playerInfo[playerid].OnlineTime + ((getTickCount () - onlineTime[playerid]) / 1000 );
    // And now format it.
    local onlineSeconds = (totalTime % 60);
    local onlineMinutes = (totalTime / 60 % 60);
    local onlineHours = (totalTime / 3600 % 24);
            
    // And send the message to the player.
    sendPlayerMessage (playerid, "You have been online for " + onlineHours + " hours, " + onlineMinutes + " minutes and " + onlineSeconds + " seconds,", COLOR_WHITE);
    sendPlayerMessage (playerid, "you have killed " + playerInfo[playerid].Kills + " players, and have died " + playerInfo[playerid].Deaths + " times yourself.", COLOR_WHITE);
    sendPlayerMessage (playerid, "You are level " + playerInfo[playerid].Level + " and you have " + playerInfo[playerid].Bank + "$ on your bank.", COLOR_WHITE);
}

cmdHandler.registerCommand ("/stats", Command (cmdStats), COMMAND_LEVEL_PLAYER);

/**
 * This is the /register command, it passes the correct
 * parameters onto the RegisterPlayer function
 * @author: Matthias <matthias@sa-mp.nl>
 *
 * @param playerid The playerid that used the command.
 * @param commandParams The parameters passed on to the command.
 * @param commandText The entire command as a string.
 */
local function cmdRegister (playerid, commandParams, commandText)
{
    // Create a new instance of the MySQL class.
    local Database = MySQL ();
    
    // Check if we still are connected.
    Database.checkConnection ();
    
    // We need to make sure the user isn't registed already.
    if (isRegistered[playerid] == true)
    {
        sendPlayerErrorMessage( playerid, "You are already registered!" );
        return;    
    }
    
    // Todo: add a nice gui, this will be available in beta 2.
    // Did the user enter a password?
    if (commandParams.len() < 3 )
    {
        sendPlayerUsageMessage (playerid, "/register [password] [email]");
        return;
    }
    
    // We get the variables from the users input.
    local Password = commandParams[1];
    local Email = commandParams[2]
    
    // Is the password long enough?
    if (Password.len() < 6)
    {
        sendPlayerErrorMessage (playerid, "Your password has to be at least 6 characters long");
        return;
    }
    
    // Todo: format checking.
    if (Email.len() < 6)
    {
        sendPlayerErrorMessage (playerid, "Your email adress has to be at least 6 characters long");
        return;
    }
    
    // Register the player.
    RegisterPlayer (playerid, Password, Email);
    
    // The player is registered.
    isRegistered[playerid] = true;
    
    // And inform him it was a succes!
    sendPlayerMessage (playerid, "* You have succesfully registered on the server. Please use /login now.", COLOR_GREEN);
}

cmdHandler.registerCommand ("/register", Command (cmdRegister), COMMAND_LEVEL_PLAYER);

/**
 * This is the /login command, this passes the
 * correct parameters onto the LogPlayerIn function.
 * @author: Matthias <matthias@sa-mp.nl>
 *
 * @param playerid The playerid that used the command.
 * @param password The password the user typed
 */
local function cmdLogin (playerid, password)
{
    // He's logged in already.
    if (isLoggedIn[playerid] == true)
    {
        sendPlayerErrorMessage (playerid, "You are already logged in!");
        return;
    }
    
    // Create a new instance of the MySQL class.
    local Database = MySQL ();
    
    // Check if we still are connected.
    Database.checkConnection ();
    
    // We need to make sure the user is actually registered.
    if (isRegistered[playerid] == true)
    {
        // Escape the password.
        local EscapedPassword = Database.escapeString (password);
        
        // Log him in.
        if (LogPlayerIn (playerid, EscapedPassword) == true)
        {
            sendPlayerMessage (playerid, "* You have been succesfully logged in." COLOR_GREEN);
            isLoggedIn[playerid] = true;
            
            togglePlayerFrozen (playerid, false);
            
            // Load the users data.
            LoadUserData (playerid);
            return;
        }
        else
        {
            // Todo: add kick system if attempts > 3
            sendPlayerErrorMessage (playerid, "Wrong password.");
            return;
        }
    }
    else
    {
        sendPlayerErrorMessage (playerid, "You haven't registered yet! Use /register to do so.");
        return;
    }
}

addEvent("LogPlayerIn", cmdLogin);